import Vue from 'vue'
import Vuex from 'vuex'
import * as $http from '../utils/http'

Vue.use(Vuex)

const state = {
  list: [],

  showMap: true
}

const getters = {
<<<<<<< HEAD
  searchByKeywordsList(state) {
    return (keywords) => {
      return state.list.filter((li) => {
        const reg = new RegExp(keywords, 'gi')
=======
  filteredByKeyWords(state) {
    return (keywords) => {
      const reg = new RegExp(keywords, 'gi')
      return state.list.filter((li) => {
>>>>>>> before-10-1
        return reg.test(li.name)
      })
    }
  },

  filteredList(state, getters) {
    return (size, keywords) => {
      if (keywords) {
<<<<<<< HEAD
        const result = getters.searchByKeywordsList(keywords)
=======
        const result = getters.filteredByKeyWords(keywords)
>>>>>>> before-10-1
        return result.slice(0, size)
      } else {
        return state.list.slice(0, size)
      }
    }
  },

<<<<<<< HEAD
  listPageSize(state, getters) {
    return (keywords) => {
      if (keywords) {
        const result = getters.searchByKeywordsList(keywords)
=======
  pageSize(state) {
    return (keywords) => {
      if (keywords) {
        const result = getters.filteredByKeyWords(keywords)
>>>>>>> before-10-1
        return Math.ceil(result.length / 10)
      } else {
        return Math.ceil(state.list.length / 10)
      }
    }
  }
}

const mutations = {
  setList(state, list) {
    state.list = list
  },

  setShowMap(state) {
    state.showMap = !state.showMap
  }
}

const actions = {
  async loadData({commit}) {
    let result = await $http.get('/list')
    commit('setList', result.data)
  },

  showMap ({commit}) {
    commit('setShowMap')
  }
}

const store = new Vuex.Store({
  state,
  getters,
  mutations,
  actions
})

export default store